package org.gruposp2p.aularest.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.gruposp2p.aularest.server.LinkConstants;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "tutor")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "TUTOR", catalog = "database", schema = "PUBLIC")
@NamedQueries({@NamedQuery(name = "Tutor.findAll", query = "SELECT t FROM Tutor t"), @NamedQuery(name = "Tutor.findById", query = "SELECT t FROM Tutor t WHERE t.id = :id"), @NamedQuery(name = "Tutor.findByName", query = "SELECT t FROM Tutor t WHERE t.name = :name"), @NamedQuery(name = "Tutor.findBySurnames", query = "SELECT t FROM Tutor t WHERE t.surnames = :surnames"), @NamedQuery(name = "Tutor.findByPhone1", query = "SELECT t FROM Tutor t WHERE t.phone1 = :phone1"), @NamedQuery(name = "Tutor.findByPhone2", query = "SELECT t FROM Tutor t WHERE t.phone2 = :phone2"), @NamedQuery(name = "Tutor.findByEmail", query = "SELECT t FROM Tutor t WHERE t.email = :email"), @NamedQuery(name = "Tutor.findByProfession", query = "SELECT t FROM Tutor t WHERE t.profession = :profession"), @NamedQuery(name = "Tutor.findByDescription", query = "SELECT t FROM Tutor t WHERE t.description = :description")})
public class Tutor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME", nullable = false, length = 48)
    private String name;
    @Basic(optional = false)
    @Column(name = "SURNAMES", nullable = false, length = 48)
    @XmlElement(name="surname")
    private String surnames;
    @Column(name = "PHONE1", length = 48)
    @XmlElement(name="phone1")
    private String phone1;
    @Column(name = "PHONE2", length = 48)
    @XmlElement(name="phone2")
    private String phone2;
    @Column(name = "EMAIL", length = 48)
    @XmlElement(name="email")
    private String email;
    @Column(name = "PROFESSION", length = 48)
    @XmlElement(name="profession")
    private String profession;
    @Column(name = "DESCRIPTION", length = 511)
    @XmlElement(name="description")
    private String description;
    @ManyToMany(mappedBy = "tutorCollection")
    private Collection<Student> studentCollection;
    @OneToMany(mappedBy = "tutorId")
    private Collection<Address> addressCollection;

    @Transient
    @XmlElement(name="tutorLink")
    private Link link;

    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.name = link.getName();
        this.link = link;
    }
    
    @Transient
    @XmlElementWrapper(name = "students")
    @XmlElement(name="studentLink")
    private Collection<Link> students;

    public Collection<Link> getStudentsLinks() {
        return students;
    }


    @Transient
    @XmlElementWrapper(name = "addresses")
    @XmlElement(name="address")
    private Collection<Link> addresses;

    public Collection<Link> getAddressesLinks () {
        return addresses;
    }

    public Tutor() {
    }

    public Tutor(Integer id) {
        this.id = id;
    }

    public Tutor(Integer id, String name, String surnames) {
        this.id = id;
        this.name = name;
        this.surnames = surnames;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurnames() {
        return surnames;
    }

    public void setSurnames(String surnames) {
        this.surnames = surnames;
    }

    public String getPhone1() {
        return phone1;
    }

    public void setPhone1(String phone1) {
        this.phone1 = phone1;
    }

    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getProfession() {
        return profession;
    }

    public void setProfession(String profession) {
        this.profession = profession;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Collection<Student> getStudentCollection() {
        return studentCollection;
    }

    public void setStudentCollection(Collection<Student> studentCollection) {
        this.studentCollection = studentCollection;
    }

    public Collection<Address> getAddressCollection() {
        return addressCollection;
    }

    public void setAddressCollection(Collection<Address> addressCollection) {
        this.addressCollection = addressCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Tutor)) {
            return false;
        }
        Tutor other = (Tutor) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.gruposp2p.restletjpa.model.Tutor[id=" + id + "]";
    }
    @PostLoad
    public void postProcess() {
        link = new Link();
        link.setId(id.toString());
        link.setHref(LinkConstants.getTutorLink(id.toString()));
        link.setName(name);
        if (studentCollection != null) {
            students = new ArrayList<Link>();
            for(Student student : studentCollection) {
                students.add(student.getLink());
            }
        }
        if (addressCollection != null) {
            addresses = new ArrayList<Link>();
            for (Address address : addressCollection) {
                addresses.add(address.getLink());
            }
        }
    }
    @PrePersist
    public void prePersist () {
        this.name = link.getName();
    }
}
